export class DownloadFile {
  static download(downloadFile: {}) {
    let fileName = this.getFileName(downloadFile['headers']);
    let href = window.URL.createObjectURL(new Blob([downloadFile['data']]));
    let aLink = document.createElement('a');
    aLink.style.display = 'none';
    aLink.href = href;
    aLink.setAttribute('download', fileName);
    document.body.appendChild(aLink);
    aLink.click();

    document.body.removeChild(aLink);
    window.URL.revokeObjectURL(href);
  }

  // Get file name by http response header content-disposition
  static getFileName(responseHeader: object): string {
    let contentDisposition = responseHeader['content-disposition'];
    return contentDisposition
      .substr(contentDisposition.indexOf('filename='))
      .split('=')[1]
      .replace(/"/g, '');
  }
}

export default DownloadFile;
